<?php
function setCommentColor($string, $key)
{
    $index = strpos($string, $key);
    if ($index !== false) {
        if ($index > 0) {
            $tmp1 = substr($string, 0, $index);
            foreach (COLOR_MAP as $k => $v) {
                $tmp1 = setKeyword($tmp1, $k, $v);
            }
            $tmp2 = substr($string, $index);
            $string = $tmp1 . "<span class='grey'>" . $tmp2 . "</span>";
        } else {
            $string = "<span class='grey'>" . $string . "</span>";
        }
    }
    return $string;
}

function setKeyword($string, $key, $v)
{
    $index = strpos($string, $key);
    if ($key !== '#') {
        $index1 = strpos($string, "#");
        if ($index1 !== false) {
            $index = false;
        }
    }
    if ($index !== false) {
        $tmp = substr($string, 0, $index);
        if (trim($tmp) === "") {
            $string = str_replace($key, "<span class='{$v}'>{$key}</span>", $string);
        }
    }
    return $string;
}

function getSpaceString($n)
{
    $string = "";
    $temp = "&nbsp;&nbsp;&nbsp;&nbsp;";
    for ($i = 0; $i < $n; $i++) {
        $string .= $temp;
    }
    return $string;
}

function getContent(string $filePath): array
{
    $i = 0;
    $originContent = $content = "";
    $fn = fopen($filePath, "r");
    while (!feof($fn)) {
        $string = fgets($fn);
        $originContent .= $string;
        if(str_contains($string, "server_name")){
            $arr = explode("server_name ", $string);
            $string = $arr[0] . "server_name " . "<a href='https://" . str_replace([";", "\r", "\n"], "", $arr[1])."' target='_blank'>" . str_replace([";", "\r", "\n"], "", $arr[1]). "</a>;";
        }
        foreach (COLOR_MAP as $k => $v) {
            $string = setKeyword($string, $k, $v);
        }
        $string = setCommentColor($string, "#");
        if (str_contains($string, "}") && !str_contains($string, "{")) {
            $i--;
        }
        $content .= getSpaceString($i) . $string . "<br>";
        if (str_contains($string, "{") && !str_contains($string, "}")) {
            $i++;
        }
    }
    fclose($fn);
    return [$originContent, $content];
}